var $$ = {};
//模态框
$$.modal={};
$$.modal.alert = function (body,btn) {
    $('#alertModal').remove();
    var html = [
        '<div id="alertModal" class="modal fade bs-example-modal-sm">',
        '<div class="modal-dialog modal-sm" style="margin-top:100px;">',
        '<div class="modal-content">',
        '<div class="modal-body">',
        '<p>'+ body +'</p>',
        '</div>',
        '<div class="modal-footer" style="border-top: 1px solid #e5e5e5;padding-top: 8px;">',
        '<button type="button" class="btn btn-raised btn-danger" data-dismiss="modal">'+ btn +'</button>',
        '</div>',
        '</div>',
        '</div>',
        '</div>'
    ].join('');
    $('body').append(html);
    $('#alertModal').modal();
};
$$.modal.confirm = function (body, btnCancel, btnEnter,callback) {
    $('#confirmModal').remove();
    var html = [
        '<div id="confirmModal" class="modal fade bs-example-modal-sm">',
        '<div class="modal-dialog modal-sm" style="margin-top:100px;">',
        '<div class="modal-content">',
        '<div class="modal-body">',
        '<p>'+ body +'</p>',
        '</div>',
        '<div class="modal-footer" style="border-top: 1px solid #e5e5e5;padding-top: 8px;">',
        '<button type="button" class="btn btn-raised btn-default cancel" data-dismiss="modal">'+ btnCancel +'</button>',
        '<button type="button" class="btn btn-raised btn-info">'+ btnEnter +'</button>',
        '</div>',
        '</div>',
        '</div>',
        '</div>'
    ].join('');
    $('body').append(html);
    $('#confirmModal').modal();
    $('#confirmModal').on('click','.btn-info',function () {
        $('#confirmModal').modal('hide');
        callback();
    })
};
$$.modal.tips = function (body,timeout,position) {
    $$.modal.tips.timer = $$.modal.tips.timer || {};
    clearTimeout($$.modal.tips.timer);
    var $tipsContainer = $('#tipsContainer');
    $tipsContainer.remove();
    var htmlWidth = $('body').width();
    var containerStyle = 'position: fixed; left: 50%; bottom: 0; z-index: 99999; word-wrap: break-word; word-break: break-all; max-width: 400px; margin-left: -30px;';
    if(htmlWidth <= 768){
        containerStyle += 'max-width:auto;width:80%;text-align:center';
    }
    position = position || containerStyle;
    var html = [
        '<div id="tipsContainer" style="'+ position +'">',
        '<div class="snackbar">',
        '<span class="snackbar-content">'+ body +'</span>',
        '</div>',
        '</div>'
    ].join('');
    $("body").append(html);
    var snackbar = $('#tipsContainer .snackbar');
    setTimeout(function () {
        snackbar.addClass('snackbar-opened');
    },50);
    $$.modal.tips.timer = setTimeout(function () {
        snackbar.removeClass('snackbar-opened');
    },timeout);
    $tipsContainer.on('click','.snackbar',function () {
        snackbar.removeClass('snackbar-opened');
        clearTimeout($$.modal.tips.timer);
    });
    //水平居中
    $('#tipsContainer').css('margin-left',-($('#tipsContainer').width() / 2))
};

//工具类
$$.util={};
//获取当前时间戳
$$.util.timeStamp = function () {
    this.pad = function(num, n) {
        var len = num.toString().length;
        while(len < n) {
            num = "0" + num;
            len++;
        }
        return num;
    };
    var now = new Date();
    return  "" + this.pad(now.getFullYear(),4)+ this.pad(now.getMonth() + 1,2) + this.pad(now.getDate(),2)
        + this.pad(now.getHours(),2) + this.pad(now.getMinutes(),2) + this.pad(now.getSeconds(),2)
        + this.pad(now.getMilliseconds(),3);
};
//毫秒转年月日（2016-05-24）
$$.util.timeStampToDay = function (timeStamp) {
    timeStamp = timeStamp || new Date().getTime();
    this.pad = function(num, n) {
        var len = num.toString().length;
        while(len < n) {
            num = "0" + num;
            len++;
        }
        return num;
    };
    var now = new Date(timeStamp);
    return  "" + this.pad(now.getFullYear(),4)+ '-' + this.pad(now.getMonth() + 1,2) + '-' + this.pad(now.getDate(),2);
};
//url参数转json
$$.util.urlParamToObj = function () {
    var urlParamObj = {};
    if(location.search.split('?')[1]){
        var urlParamList = location.search.split('?')[1].split('&');
        urlParamList.map(function (a) { return urlParamObj[a.split('=')[0]] = a.split('=')[1];});
    }
    return urlParamObj;
};

//模态框垂直居中
$('body').on('shown.bs.modal','.modal',function (e) {
    var that = $(this);
    if(that.hasClass('top')) {
        that.addClass('active');
        return false;
    }
    var topValue = (($(window).height() - that.find('.modal-dialog').height())/2);
    if(topValue < 10) topValue = 10;
    that.find('.modal-dialog').css({'margin-top' :topValue});
    that.addClass('active');
});
